package com.nj9you.sdk;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import com.nj9you.sdk.framework.ResponseCallback;
import com.nj9you.sdk.http.Response;
import com.nj9you.sdk.params.PayParams;
import com.nj9you.sdk.params.User;
import com.nj9you.sdk.request.IPayRequest;
import com.nj9you.sdk.request.IUserRequest;
import com.nj9you.sdk.request.PayRequestImpl;
import com.nj9you.sdk.request.UserRequestImpl;

import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.nj9you.sdk.test", appContext.getPackageName());
    }

    /**
     * 测试登录接口
     */
    @Test
    public void testLogin() {
        final CountDownLatch signal = new CountDownLatch(1);
        Context appContext = InstrumentationRegistry.getTargetContext();
        IUserRequest userRequest = new UserRequestImpl(appContext);
        userRequest.login("15295585114", "taugin0426", new ResponseCallback<User>() {
            @Override
            public void onCallback(Response<User> response) {
                assertNotNull(response);
                signal.countDown();
            }
        });
        try {
            signal.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试检测用户名是否可以注册
     */
    @Test
    public void testCheckRegisterUser() {
        final CountDownLatch signal = new CountDownLatch(1);
        Context appContext = InstrumentationRegistry.getTargetContext();
        IUserRequest userRequest = new UserRequestImpl(appContext);
        userRequest.checkRegisterUser("song1shun110", new ResponseCallback<Void>() {
            @Override
            public void onCallback(Response<Void> response) {
                assertEquals(response.getCode(), "00");
                signal.countDown();
            }
        });
        try {
            signal.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testGetAlipayArgs() {
        final CountDownLatch signal = new CountDownLatch(1);
        Context appContext = InstrumentationRegistry.getTargetContext();
        IPayRequest payRequest = new PayRequestImpl(appContext);
        PayParams payParams = new PayParams();
        payParams.setSubject("一元夺宝");
        payParams.setPrice("1");
        payParams.setAttach("njjy_alipay");
        payParams.setBody("一元夺宝描述");
        payRequest.queryAlipayArgs(payParams, new ResponseCallback<JSONObject>() {
            @Override
            public void onCallback(Response<JSONObject> response) {
                assertEquals(response.getCode(), "00");
                signal.countDown();
            }
        });
        try {
            signal.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
